/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "oled.h"
#include "esp8266.h"
#include "stdbool.h"
#include "string.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
char RxBuffer[256];
uint8_t aRxBuffer1;			
uint8_t Uart2_Rx_Cnt = 0;		
char my_order[15]={0};
char receive_flag=0;
uint8_t tx_buf[30];
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
	uint8_t len;
	uint8_t breath=0;
	uint8_t heart=0;
	uint8_t move=0;
	int distance=0;
	uint8_t key=0;
	uint8_t menu=1;
	uint8_t report_flag=0;
	bool warning_flag=0;
	uint8_t heart_warning=100;//心率报警
	uint8_t breath_warning=60;//呼吸报警
	uint8_t move_warning=100;//体动报警
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  MX_USART3_UART_Init();
  /* USER CODE BEGIN 2 */
	HAL_UART_Receive_IT(&huart2, (uint8_t *)aRxBuffer, RXBUFFERSIZE);
	HAL_UART_Receive_IT(&huart3, (uint8_t *)&aRxBuffer, 1);
	esp8266_start_trans();
	OLED_Init();
//	OLED_ShowString(16, 18, "Hello World!", OLED_6X8);
//	OLED_Update();
//	HAL_UART_Transmit(&huart2,"hello",5,1000);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
		key=key_scan(0);
		if(menu==1)
		{
			OLED_ShowChinese(10, 0, "心率");
			OLED_Printf(74, 0, OLED_8X16, "%d  ", heart);
			OLED_ShowChinese(10, 16, "呼吸率：");
			OLED_Printf(74, 16, OLED_8X16, "%d      ", breath);
			OLED_ShowChinese(10, 32, "距离：");
			OLED_Printf(74, 32, OLED_8X16, "%d    ", distance);
			OLED_ShowChinese(10, 48, "体动率：");
			OLED_Printf(74, 48, OLED_8X16, "%d        ", move);
			strcpy((char *)tx_buf,"000#000#000#000#\r\n");
			tx_buf[0]=heart/100+'0';
			tx_buf[1]=heart/10%10+'0';
			tx_buf[2]=heart%10+'0';
			tx_buf[4]=breath/100+'0';
			tx_buf[5]=breath/10%10+'0';
			tx_buf[6]=breath%10+'0';
			tx_buf[8]=distance/100+'0';
			tx_buf[9]=distance/10%10+'0';
			tx_buf[10]=distance%10+'0';			
			tx_buf[12]=move/100+'0';
			tx_buf[13]=move/10%10+'0';
			tx_buf[14]=move%10+'0';
			u2_printf("AT+CIPSEND=0,20\r\n");
			HAL_Delay(100);
			u2_printf((char *)tx_buf);
			if(report_flag==0)
			{
				if(USART_RX_STA&0x8000)
				{
					len=(USART_RX_STA+1)&0x3fff;
					if(len==10)                       
					{
						if(USART_RX_BUF[2]==0x80&&USART_RX_BUF[3]==0x03&&USART_RX_BUF[4]==0x00&&USART_RX_BUF[5]==0x01)//体动率
						{
							move=USART_RX_BUF[6];
						}
						else if(USART_RX_BUF[2]==0x85&&USART_RX_BUF[3]==0x02&&USART_RX_BUF[4]==0x00&&USART_RX_BUF[5]==0x01)//心跳
						{
							heart=USART_RX_BUF[6];
						}
						else if(USART_RX_BUF[2]==0x81&&USART_RX_BUF[3]==0x02&&USART_RX_BUF[4]==0x00&&USART_RX_BUF[5]==0x01)//呼吸率
						{
							breath=USART_RX_BUF[6];
						}
					}
					else if(len==11)
					{
						if(USART_RX_BUF[2]==0x80&&USART_RX_BUF[3]==0x04&&USART_RX_BUF[4]==0x00&&USART_RX_BUF[5]==0x02)//人体距离
						{
							distance = USART_RX_BUF[6]*256+USART_RX_BUF[7];
						}
					}
					USART_RX_STA=0;
				}
			}
			if(warning_flag==1)
			{
				OLED_ShowImage(112, 0, 16, 16, Diode);
				if(heart>heart_warning||move>move_warning||breath>breath_warning)
				{
					menu=6;
					OLED_Clear();
				}
			}
			else if(warning_flag==0)
			{
				BEEPOFF();
				OLED_ClearArea(112, 0, 16, 16);
			}
			if(key==1)
			{
				menu=2;
				OLED_Clear();
				report_flag=1;
			}
			else if(key==4)
			{
				warning_flag=!warning_flag;
			}
		}
		else if(menu==2)
		{
			OLED_ShowChinese(20, 0, "数据上报模式");
			OLED_ShowChinese(0, 32, "数据上报中。。。");
			if(report_flag==1)
			{
				 if(USART_RX_STA&0x8000)
				 {					   
					len=USART_RX_STA&0x3fff;//接收长度
					HAL_UART_Transmit(&huart1,(uint8_t*)USART_RX_BUF,len+1,1000);
					while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)!=SET);
					printf("\r\n");
					USART_RX_STA=0;
				 }
			}
			if(key==1)
			{
				menu=3;
				OLED_Clear();
				report_flag=0;				
			}
		}
		else if(menu==3)
		{
			OLED_ShowChinese(10, 16, "心率报警阈值：");
			OLED_Printf(50, 32, OLED_8X16, "%d  ", heart_warning);
			if(key==2)
			{
				heart_warning--;
			}
			else if(key==3)
			{
				heart_warning++;
			}
			else if(key==1)
			{
				menu=4;
				OLED_Clear();
			}
		}
		else if(menu==4)
		{
			OLED_ShowChinese(10, 16, "呼吸报警阈值：");
			OLED_Printf(50, 32, OLED_8X16, "%d  ", breath_warning);
			if(key==2)
			{
				breath_warning--;
			}
			else if(key==3)
			{
				breath_warning++;
			}
			else if(key==1)
			{
				menu=5;
				OLED_Clear();
			}
		}
		else if(menu==5)
		{
			OLED_ShowChinese(10, 16, "体动报警阈值：");
			OLED_Printf(50, 32, OLED_8X16, "%d  ", move_warning);
			if(key==2)
			{
				move_warning--;
			}
			else if(key==3)
			{
				move_warning++;
			}
			else if(key==1)
			{
				menu=1;
				OLED_Clear();
			}
		}
		else if(menu==6)
		{
			if(key==1)
			{
				warning_flag=0;
				BEEPOFF();
				OLED_Clear();
				menu=1;
			}
			if(move>move_warning)
			{
				OLED_ShowChinese(20, 16, "体动异常！");
				BEEPON();
			}
			else if(breath>breath_warning)
			{
				OLED_ShowChinese(20, 16, "呼吸异常！");
				BEEPON();
			}
			else if(heart>heart_warning)
			{
				OLED_ShowChinese(20, 16, "心率异常！");
				BEEPON();
			}
		}
		OLED_Update();	
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
